//
//  SettingViewController.m
//  Stock
//
//  Created by blacksun on 11-7-9.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import "CommonUtil.h"


@implementation SettingViewController
@synthesize tableView;
@synthesize cellTitles;



- (void)dealloc
{
    self.tableView = nil;
    self.cellTitles = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    NSArray *title1 = [NSArray arrayWithObjects:@"软件版本",@"免责声明",@"给软件评分",IsPaidVersion?nil:@"升级到专业版",nil];
    NSArray *title2 = [NSArray arrayWithObjects:@"邮箱",@"QQ群",nil];
    self.cellTitles = [NSArray arrayWithObjects:title1,title2, nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    self.cellTitles = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[cellTitles objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"SettingViewCell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, 80.0, 20.0)];
        leftLabel.tag = 999;
        [cell.contentView addSubview:leftLabel];
        [leftLabel release];
        
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(80.0, 12.0, 210.0, 20.0)];
        rightLabel.tag = 10000;
        //rightLabel.textColor = [UIColor blackColor];
        rightLabel.textAlignment = UITextAlignmentRight;
        [cell.contentView addSubview:rightLabel];
        [rightLabel release];
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    UILabel *leftLabel = (UILabel *)[cell viewWithTag:999];
    UILabel *rightLabel = (UILabel *)[cell viewWithTag:10000];
    rightLabel.hidden = YES;
    if (indexPath.section==0 && indexPath.row==0) {
        rightLabel.hidden = NO;
        rightLabel.text = @"2.0";
    }else if(indexPath.section==1){
        if (indexPath.row==0) {
            rightLabel.hidden = NO;
            rightLabel.text = @"ipoint.mobile@hotmail.com";
        }else if(indexPath.row==1){
            rightLabel.hidden = NO;
            rightLabel.text = @"68398080";
        }
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    leftLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [leftLabel sizeToFit];
    //cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return @"软件说明";
    }
    return @"联系我们";
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==1&&indexPath.row==0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://ipoint.mobile@hotmail.com"]];
    }else if(indexPath.section==0){
        if (indexPath.row==0) {
            return;
        }else if (indexPath.row==1) {
            SettingWebviewController *webViewController = [[SettingWebviewController alloc] initWithNibName:@"SettingWebviewController" bundle:nil];
            NSString *htmlString= [NSString stringWithContentsOfFile:[[CommonUtil shareInstance] pathOfResouceInMainBundle:@"about.html"] encoding:NSUTF8StringEncoding error:nil];
            webViewController.htmlString = htmlString;
            
            webViewController.title = [[self.cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:webViewController animated:YES];
            [webViewController release];
        }else{
            NSString *appStoreURL = nil;
            if (indexPath.row==2) {
                appStoreURL = AppStoreLink;
            }else{
                appStoreURL = ProVersionAppStoreLink;
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStoreURL]];
        }
    }
}
@end
